<?php

// Business class to include

class Admin_LoginController extends SedController
{
	/*
	 * Log in
	 */
	function loginAction()
	{	
		$smarty = FRM::load('smarty');		
		
		
		if (FRM_Request::isParamSet("login")) 
		{
			//check user login
			$error_code = User::performLogin($_POST["login"], $_POST["pwd"]);
		}
		
		if (!FRM_Session::exist('user_serialize_admin'))
		{
			$smarty->assign("login", (FRM_Request::isParamSet('login') ? FRM_Request::get('login') : ""));
			$smarty->display('admin/login.html');
		}
		else
		{
			//login successful
			FRM::redirect(APPLICATION_URL . '/admin/index');			
		}
	}
	
	/*
	 * Logout
	 */
	function logoutAction()
	{
		// check whether or not the current login user was Admin and it was logged by Login OneClick
		$cur_login_user = unserialize(FRM_Session::get('user_serialize_admin'));
		$user = new User();
		if ($cur_login_user->is_controller == 1 
			&& FRM_Session::exist('retransform_id') && $user->get(FRM_Session::get('retransform_id')))
		{
			FRM_Session::remove();

			// this will be used to clarify that next is retransforming login action
			FRM_Request::set('is_retransform', 1);
			FRM_Request::set('id', $user->id);
			self::loginAction();
		}
		else 
		{
			FRM_Session::remove();
		}
		
		FRM::redirect(APPLICATION_URL . '/login/login');
	}
	
	/**
	 * Switch to Admin site
	 *
	 */
	function switchToAdminSiteAction()
	{
		// get id of user
		$id = FRM_Request::getIntNumber('id');

		// get site type
		$site_type = FRM_Request::getIntNumber('site');

		// switch to site
		User::switchByOneClick($id);	
	}
	
	/**
	 * Switch to Advertiser site
	 *
	 */
	function switchToAdvertiserSiteAction()
	{
		// get id of user
		$id = FRM_Request::getIntNumber('id');

		// switch to site
		User::switchByOneClick($id, User::APPLICATION_ADVERTISER);	
	}
	
	/**
	 * Switch to Editor site
	 *
	 */
	function switchToEditorSiteAction()
	{
		// get id of user
		$id = FRM_Request::getIntNumber('id');

		// switch to site
		User::switchByOneClick($id, User::APPLICATION_EDITOR);		
	}	
}
